package com.cn.por.util.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;

/**
 * javaweb 获取文件
 * @author 26983
 *
 */
public class WebGetFile {
	
	@GetMapping("/getFile")
    public void getFile(HttpServletResponse response) {
    	ClassLoader classLoader = WebGetFile.class.getClassLoader();
		response.reset();
        response.setHeader("content-disposition", "attachment;filename=" + "1.xlsx");
        response.setContentType("application/octet-stream");
        InputStream in =  classLoader.getResourceAsStream("pdf/1.xlsx");
        int len = 0;
        byte[] buffer = new byte[1024];
        OutputStream out;
		try {
			out = response.getOutputStream();
			 while ((len = in.read(buffer)) > 0) {
	            //将缓冲区的数据输出到客户端浏览器
	            out.write(buffer, 0, len);
	            out.flush();
	        }
			out.close();
		    in.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
       
        
    }

}
